package basic.study.wantOffer.chapter6;

/**
 * @ClassName Problem63
 * @Description 股票的最大利润
 * @Company inspur
 * @Author Kevin
 * @Date 2020/6/15 10:21
 * @Version 1.0
 */
public class Problem63 {
    public static int maxDiff(int[] arr) {
        if (arr == null || arr.length <= 0) {
            return -1;
        }
        if (arr.length == 1) {
            return arr[0];
        }
        int min = arr[0];
        int max_diff = arr[1] - arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i-1] < min) {
                min = arr[i-1];
            }
            if (arr[i] - min > max_diff) {
                max_diff = arr[i] - min;
            }
        }
        return max_diff;
    }

    public static void main(String[] args) {
        System.out.println(maxDiff(new int[]{2,1,3,4,5,6,7,8}));
        System.out.println(maxDiff(new int[]{16,16,16,16}));
    }
}
